package com.edu.oma.protocol.redis;

import com.alibaba.fastjson.JSON;
import com.edu.oma.protocol.common.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;


@Component
public class RedisClientImpl implements RedisClient{

	private Logger logger = LoggerFactory.getLogger(RedisClientImpl.class);
    
    @SuppressWarnings("rawtypes")
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 根据key获取值
     *
     * @param key
     * @return object
     */
    @SuppressWarnings("unchecked")
	public String get(String key) {
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        Object value = operations.get(key);
        if(value != null){
        	return value.toString();
        }else{
        	return null;
        }
    }

    @Override
	public boolean hasKey(String key) {
		boolean result = false;
        try {
        	result = redisTemplate.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
	}
    
    @SuppressWarnings("unchecked")
	public boolean setKeyValue( String key, String value){
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    @SuppressWarnings("unchecked")
	public boolean setKeyValueAndCommExpired( String key, String value){
        return setKeyValueAndExpired( key, value, Constant.REDIS_EXPIRE_TIME_COMMON);
    }

    @SuppressWarnings("unchecked")
    public boolean setKeyValueAndExpired( String key, String value, Long second){
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            //添加过期时间
            redisTemplate.expire(key, second, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * hash 写入缓存
     *
     */
    public boolean setHashAndExpired(String key, String field,String value, Long seconds) {
        boolean result = false;
        try {
            HashOperations<Serializable, String, String> operations = redisTemplate.opsForHash();
            operations.put(key, field, value);
            redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }
    public boolean setHashAndCommExpired(String key, String field,String value) {
        return setHashAndExpired(key, field, value, Constant.REDIS_EXPIRE_TIME_COMMON);

    }

    public String getHash(String key, String field) {
        HashOperations<Serializable, String, String> operations = redisTemplate.opsForHash();
        Object o = operations.get(key, field);
        if(o!= null){
            return o.toString();
        }else{
            return null;
        }
    }

    public void deleteHash(String key) {
        redisTemplate.delete(key);
    }

}
